import requests
import time
from bs4 import BeautifulSoup
import re
import json
import urllib


#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []


    lis = response.json()["results"]

    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li["PartTitle"]
        except:
            pass
        try:
            tempData["titleHref"] = "http://methods.sagepub.com" + li["PartUrl"]
        except:
            pass

        try:
            tempData["author"] = [li["People"]]
        except:
            pass

        try:
            tempData["keyword"] = li["Keywords"]
        except:
            pass

        try:
            tempData["abstract"] = li["Snippet"]
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword):


    headers = {
        'authority': 'onlinelibrary.wiley.com',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'referer': 'https://onlinelibrary.wiley.com/',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        # 'cookie': '_ga=GA1.2.320146595.1542014210; __gads=ID=ae68195e424f1d5a:T=1542801528:S=ALNI_MZAbw28FdGBOXaRNkE7KXCDzNdMog; MAID=kB3EAH23qvC/hXTPEnQenQ==; SERVER=WZ6myaEXBLHrdNmd/vcq8Q==; SERVER=WZ6myaEXBLHrdNmd/vcq8Q==; MAID=kB3EAH23qvC/hXTPEnQenQ==; MACHINE_LAST_SEEN=2019-01-29T22%3A07%3A54.477-08%3A00; MACHINE_LAST_SEEN=2019-01-29T22%3A07%3A54.477-08%3A00; JSESSIONID=aaasBeeMmTJT5edXWOmIw; JSESSIONID=aaasBeeMmTJT5edXWOmIw; _sdsat_MCID=85852668883443686643298823647410017353; randomizeUser=0.5811149452062663; AMCVS_1B6E34B85282A0AC0A490D44%40AdobeOrg=1; AMCV_1B6E34B85282A0AC0A490D44%40AdobeOrg=-1303530583%7CMCIDTS%7C17927%7CMCMID%7C85852668883443686643298823647410017353%7CMCAAMLH-1549433276%7C11%7CMCAAMB-1549433276%7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y%7CMCOPTOUT-1548835676s%7CNONE%7CMCAID%7CNONE%7CvVersion%7C3.3.0; s_cc=true; _gid=GA1.2.1260528486.1548828477; _gat_wolga=1; s_sq=wileyonlinelibrary-literatum%3D%2526c.%2526a.%2526activitymap.%2526page%253Dhttps%25253A%25252F%25252Fonlinelibrary.wiley.com%25252F%2526link%253DSearch%2526region%253Dpb-page-content%2526.activitymap%2526.a%2526.c%2526pid%253Dhttps%25253A%25252F%25252Fonlinelibrary.wiley.com%25252F%2526oid%253DSearch%2526oidt%253D3%2526ot%253DSUBMIT',
    }

    params = (
        ('AllField', 'cell'),
    )

    response = requests.get('https://onlinelibrary.wiley.com/action/doSearch', headers=headers, params=params)

    allData = formData(response)


    return allData


def index():

    data = json.loads(request.POST.get('data', '1'))

    # cookies = getOnoCookies()
    allData = getData(data["values"][0])

    print(allData)

    # exit()




index()